package cn.spream.common.util;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;

import java.awt.*;

/**
 * Jmagick 处理工具类
 * Created with IntelliJ IDEA.
 * User: shaojiaxin
 * Date: 13-9-5
 * Time: 上午9:55
 * To change this template use File | Settings | File Templates.
 */
public class JmagickUtils {

    static {
        //不能漏掉这个，不然jmagick.jar的路径找不到
        System.setProperty("jmagick.systemclassloader", "no");
    }

    /**
     * 调整图片大小
     *
     * @param source 输入图片路径
     * @param target 输出图片路径
     * @param wh     {宽, 高}
     * @return
     * @throws MagickException
     */
    public static boolean resize(byte[] source, String target, int[] wh) throws MagickException {
        boolean result = false;
        MagickImage scaled = null;
        try {
            ImageInfo imageInfo = new ImageInfo();
            MagickImage magickImage = new MagickImage(imageInfo, source);
            scaled = magickImage.scaleImage(wh[0], wh[1]);
            scaled.setFileName(target);
            scaled.writeImage(imageInfo);
        } finally {
            destroyImages(scaled);
        }
        return result;
    }

    /**
     * 调整图片大小
     *
     * @param source 输入图片路径
     * @param target 输出图片路径
     * @param wh     {宽, 高}
     * @return
     * @throws MagickException
     */
    public static boolean resize(String source, String target, int[] wh) throws MagickException {
        boolean result = false;
        MagickImage scaled = null;
        try {
            ImageInfo imageInfo = new ImageInfo(source);
            MagickImage magickImage = new MagickImage(imageInfo);
            scaled = magickImage.scaleImage(wh[0], wh[1]);
            scaled.setFileName(target);
            scaled.writeImage(imageInfo);
        } finally {
            destroyImages(scaled);
        }
        return result;
    }

    /**
     * 裁剪图片
     *
     * @param source 输入图片路径
     * @param target 输出图片路径
     * @param xy     距离订单的坐标{x，y}
     * @param wh     {宽, 高}
     * @return
     * @throws MagickException
     */
    public static boolean crop(byte[] source, String target, int xy[], int[] wh) throws MagickException {
        boolean result = false;
        MagickImage scaled = null;
        try {
            ImageInfo imageInfo = new ImageInfo();
            MagickImage magickImage = new MagickImage(imageInfo, source);
            Rectangle rectangle = new Rectangle(xy[0], xy[1], wh[0], wh[1]);
            scaled = magickImage.cropImage(rectangle);
            scaled.setFileName(target);
            scaled.writeImage(imageInfo);
        } finally {
            destroyImages(scaled);
        }
        return result;
    }

    /**
     * 裁剪图片
     *
     * @param source 输入图片路径
     * @param target 输出图片路径
     * @param xy     距离订单的坐标{x，y}
     * @param wh     {宽, 高}
     * @return
     * @throws MagickException
     */
    public static boolean crop(String source, String target, int xy[], int[] wh) throws MagickException {
        boolean result = false;
        MagickImage scaled = null;
        try {
            ImageInfo imageInfo = new ImageInfo(source);
            MagickImage magickImage = new MagickImage(imageInfo);
            Rectangle rectangle = new Rectangle(xy[0], xy[1], wh[0], wh[1]);
            scaled = magickImage.cropImage(rectangle);
            scaled.setFileName(target);
            scaled.writeImage(imageInfo);
        } finally {
            destroyImages(scaled);
        }
        return result;
    }

    /**
     * 释放资源
     *
     * @param magickImage
     */
    private static void destroyImages(MagickImage magickImage) {
        if (magickImage != null) {
            magickImage.destroyImages();
        }
    }

}
